#!/usr/bin/env bash
set -uo pipefail

#==============================================================#
# File      :   bootstrap.sh
# Ctime     :   2019-09-17
# Mtime     :   2019-09-17
# Desc      :   bootstrap local yum cache
# Note      :   run this as root
# Path      :   {{ repo_home }}/{{ repo_name }}/bootstrap.sh
# Copyright (C) 2018-2022 Ruohang Feng
#==============================================================#

PROG_DIR="$(cd $(dirname $0) && pwd)"
PROG_NAME="$(basename $0)"

function log_info() {
    [ -t 2 ] && printf "\033[0;32m[$(date "+%Y-%m-%d %H:%M:%S")][INFO] $*\033[0m\n" >&2 ||
        printf "[$(date "+%Y-%m-%d %H:%M:%S")][INFO] $*\n" >&2
}

REPO_DIR="{{ repo_home }}/{{ repo_name }}"
log_info "bootstrap local yum repo ${REPO_DIR}"

# remove all existing repo
log_info "backup existing repos"
ls /etc/yum.repos.d
mkdir -p /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup

# install local repo

cat >/etc/yum.repos.d/local.repo <<-EOF
[local]
name=local
baseurl=file://${PROG_DIR}
enabled=1
gpgcheck=0
EOF

# flush yum cache
log_info "remake yum cache"
yum clean all
yum makecache
yum repolist all

# install ansible
log_info "install ansible"
yum install -y -q ansible

log_info "clean up"
rm -rf /etc/yum.repos.d/local.repo
mv -f /etc/yum.repos.d/backup/* /etc/yum.repos.d
rm -rf /etc/yum.repos.d/backup

